package util;

import jdk.internal.org.objectweb.asm.tree.MultiANewArrayInsnNode;
import jdk.nashorn.internal.scripts.JD;

import javax.xml.transform.Result;
import java.sql.*;

/**
 * description
 * created by A on 2022/4/11
 */
public class JDBCUtil {
     String url = "jdbc:postgresql://10.7.221.38:5432/edg231";
     String username = "postgres";
     String password = "postgres";
     Connection connection = null;

    public Connection getConnection() {
        try {
            Class.forName("org.postgresql.Driver").newInstance();
            connection = DriverManager.getConnection(url, username, password);

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     *
     * @param conn 链接对象
     * @param sql 查询sql
     * @return  查询结果
     */
    public ResultSet query(Connection conn,String sql){
        PreparedStatement pStatement=null;
        ResultSet rs=null;
        try{
            pStatement = conn.prepareStatement(sql);
            rs = pStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    public static void main(String[] args) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        Connection connection = jdbcUtil.getConnection();
        ResultSet query = jdbcUtil.query(connection,"select * from edac_assetbase_sjzx");
        try{
            while (query.next()){
                String id = query.getString("id_");
                String status = query.getString("status_");
                String publishpath = query.getString("publishpath_");
                System.out.println(id+":"+status+":"+publishpath);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
